package com.example.pt.controller;

import com.example.pt.entity.Notification;
import com.example.pt.dto.ApiResponse;
import com.example.pt.service.NotificationService;
import com.example.pt.service.impl.NotificationServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/user")
public class NotificationController {
    @Autowired
    private NotificationService notificationService;
    private NotificationServiceImpl notificationServiceImpl;

    // 根据 user_id 获取通知
    @GetMapping("/notifications/{user_id:\\d+}")
    public ApiResponse<List<Notification>> getNotificationById(@PathVariable("user_id") Long userId) {
        List<Notification> notification = notificationService.getNotificationById(userId);
        return ApiResponse.success(notification);
    }


    // 创建通知
    @PostMapping("/notifications/create")
    public ApiResponse createNotification(@RequestBody Notification notification) {
        notificationService.createNotification(notification);
        return ApiResponse.success("创建成功"); // 返回成功响应
    }

    // 删除通知
    @PostMapping("/notifications/delete")
    public ApiResponse deleteNotification(@RequestBody Notification notification) {
        notificationService.deleteNotification(notification.getId());
        return ApiResponse.success("删除成功");
    }

    //更新通知状态
    @PostMapping("/notifications/read")
    public ApiResponse<String> markAllAsRead(@RequestBody Notification notification) {
        String result = notificationService.updateNotificationStatus(notification.getId(),notification.getStatus());

        if (result.contains("已")) {
            return ApiResponse.success(result);  // 成功提示语
        } else {
            return ApiResponse.fail(result);    // 错误提示语
        }
    }
}